<template>
    <el-card shadow="hover">
        <el-tabs @tab-remove="removeTab" type="border-card" v-model="activeTab">
            <!--列表-->
            <el-tab-pane :closable='false' name="list">
                <span slot="label"><svg-icon icon-class="list"></svg-icon> 角色列表</span>
                <el-form :inline="true">
                    <el-form-item>
                        <el-input @keyup.enter.native="getList" clearable placeholder="角色名称"
                                  v-model="query.name"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="getList" icon="el-icon-search" type="primary">搜索</el-button>
                    </el-form-item>
                </el-form>
                <el-table
                    :data="data.list"
                    border
                    fit
                    highlight-current-row
                    style="width: 100%;"
                    v-loading="data.tableLoading"
                >
                    <el-table-column type="index" width="50"></el-table-column>
                    <el-table-column :align="data.commonAlign" label="角色名称" prop="name"
                                     width="200px"></el-table-column>
                    <el-table-column :align="data.commonAlign" label="备注" prop="memo"
                                     width="450px"></el-table-column>
                    <el-table-column :align="data.commonAlign" label="创建人" width="150px">
                        <template slot-scope="scope">
                            {{ scope.row.creatorName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="data.commonAlign" label="创建时间" prop="createTime"
                                     width="200px"></el-table-column>

                    <el-table-column :align="data.commonAlign" class-name="small-padding fixed-width" label="操作"
                                     min-width="300px">
                        <template slot-scope="scope">
                            <el-button-group>
                                <el-button @click="handleUpdate(scope.row)" type="primary">编辑</el-button>
                                <el-button @click="openAssignPermissionsDialog(scope.row)" type="primary">
                                    分配权限
                                </el-button>
                                <el-button @click="handleDelete(scope.row)" type="danger">
                                    删除
                                </el-button>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination :layout="data.layout" :limit.sync="query.pageSize" :page-sizes="data.pageSizes"
                            :page.sync="query.page" :total="query.total" @pagination="getList"
                            v-show="query.total > query.pageSize"/>
            </el-tab-pane>
            <!--新增tab-->
            <el-tab-pane :closable='false' name="add">
                <span slot="label"><svg-icon icon-class="add"></svg-icon> 新增</span>
                <role-form @handle-update="saveHandle" type="ADD"></role-form>
            </el-tab-pane>
            <!--编辑tabs-->
            <el-tab-pane :key="tab.name" :name="tab.name" closable v-for="tab in tabs">
                <span slot="label"><svg-icon icon-class="edit"></svg-icon> {{tab.label}}</span>
                <role-form :obj="tab.obj" @handle-update="updateHandle" type="UPDATE"></role-form>
            </el-tab-pane>
        </el-tabs>
        <!--分配权限弹窗-->
        <el-dialog :visible.sync="dialogAssignPermissionsVisible" title="分配权限">
            <el-tree
                :data="permissionTree"
                :default-checked-keys="rolePermission"
                :props="{
                          label: 'name',
                          children: 'children'
                     }"
                node-key="mark"
                ref="tree"
                show-checkbox></el-tree>
            <div class="dialog-footer" slot="footer">
                <el-button @click="dialogAssignPermissionsVisible = false">取消</el-button>
                <el-button @click="assignPermissions" type="primary">保存</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>

<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import axios from 'axios';

import Pagination from '@/components/Pagination/index.vue';
import RoleForm from './components/Form.vue';

import {confirmDelete, success} from '@/utils/MessageUtils';
import Data from '@/class/Data';
import Query from '@/views/role/class/Query';
import RoleTab from '@/views/role/class/RoleTab';
import {del, list, permissionTree, rolePermission, save, update, updatePermission} from '@/views/role/api';
import Obj from './class/Role';
import {getTabEditName, removeTab} from '@/utils/TabUtils';
import {UserState} from '@/store/modules/User';

@Component({
    components: {
        Pagination,
        RoleForm
    },
})
export default class Role extends Vue {

    public $refs!: {
        dataForm: HTMLFormElement,
        tree: HTMLFormElement,
    };

    private activeTab = 'list';
    private data = new Data();
    private query = new Query();
    private dialogAssignPermissionsVisible = false;
    private permissionTree = [];
    private rolePermission = [];
    private id = '';
    private tabs: RoleTab[] = [];

    private created() {
        this.getList();
    }

    private getList() {
        this.data.tableLoading = true;
        list(this.query).then((resp: any) => {
            const data = resp.data;
            this.data.list = data.rows;
            this.query.total = data.total;

            this.data.tableLoading = false;
        });
    }

    private removeTab(tabName: string) {
        if (this.activeTab === tabName) {
            this.activeTab = 'list';
        }
        this.tabs = removeTab(this.tabs, tabName);
    }

    private saveHandle(data: Obj) {
        save(data).then((resp) => {
            this.editSuccess(resp.data.message);
            this.activeTab = 'list';
        });
    }

    private handleUpdate(row: any) {
        const {id, name, memo} = row;
        const tabs = this.tabs.filter(tab => tab.name === `edit-${id}`);
        if (tabs.length === 0) {
            const tab = {
                name: getTabEditName(id),
                label: `编辑-${name}`,
                obj: {id, name, memo},
            };
            this.tabs.push(tab);
            this.activeTab = tab.name;
        } else {
            this.activeTab = tabs[0].name;
        }
    }

    private updateHandle(data: Obj) {
        update(data).then((resp) => {
            this.editSuccess(resp.data.message);
            this.removeTab(this.activeTab);
            this.activeTab = 'list';
        });
    }

    private editSuccess(message: string) {
        success('成功', message);
        this.data.dialogFormVisible = false;
        this.getList();
    }

    private handleDelete(row: any) {
        const that = this;
        confirmDelete('删除提示', `${row.name}将要被删除`, '确定', '取消', () => {
            del(row.id).then((resp: any) => {
                success('成功', resp.data.message);
                that.getList();
            });
        });
    }

    private openAssignPermissionsDialog(row: any) {
        const that = this;
        axios.all([permissionTree(), rolePermission(row.id)])
            .then(axios.spread((permissionTreeResp, rolePermissionResp) => {
                that.permissionTree = permissionTreeResp.data;
                that.rolePermission = rolePermissionResp.data;
                that.dialogAssignPermissionsVisible = true;
                that.id = row.id;
            }));
    }

    private assignPermissions() {
        if (this.id) {
            updatePermission(this.id, this.$refs.tree.getCheckedKeys()).then((resp: any) => {
                success('成功', resp.data.message);
                UserState.resetRouter();
                this.dialogAssignPermissionsVisible = false;
            });
        }
    }
}
</script>

<style scoped>

</style>
